Skip to content

[Spring] fix required constructor missing setter to parent#23925

Open
jpfinne wants to merge 14 commits into
OpenAPITools:masterfrom
jpfinne:PR23918
Open

[Spring] fix required constructor missing setter to parent#23925
jpfinne wants to merge 14 commits into
OpenAPITools:masterfrom
jpfinne:PR23918

Conversation

@jpfinne
Copy link
Copy Markdown
Contributor

@jpfinne jpfinne commented Jun 2, 2026

fixes #23918

In the required argument constructor, call the chain setters for the required fields that are not optional in the parent model.
Fix the value not initialized in the constructor.

The code works whether useOptional is true or false.

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@cachescrubber (2022/02) @welshm (2022/02) @MelleD (2022/02) @atextor (2022/02) @manedev79 (2022/02) @javisst (2022/02) @borsch (2022/02) @banlevente (2022/02) @Zomzog (2022/09) @martin-mfg (2023/08)


Summary by cubic

Fixes the Spring generator so required-args constructors in child models call the parent’s chain setters when the child requires a field that’s optional in the parent. This ensures correct initialization and works with useOptional.

Written for commit 2b11518. Summary will update on new commits.

Review in cubic

@jpfinne jpfinne marked this pull request as ready for review June 3, 2026 14:03
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 8 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java:792">
P2: Potentially incomplete inherited-property detection: only direct parent is checked, which may miss optional properties from higher ancestors in multi-level inheritance.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

if (codegenModel.getParentModel() != null) {
codegenModel.parentRequiredVars = new ArrayList<>(codegenModel.getParentModel().requiredVars);

codegenModel.requiredVarsOptionalInParent = codegenModel.requiredVars.stream()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Potentially incomplete inherited-property detection: only direct parent is checked, which may miss optional properties from higher ancestors in multi-level inheritance.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java, line 792:

<comment>Potentially incomplete inherited-property detection: only direct parent is checked, which may miss optional properties from higher ancestors in multi-level inheritance.</comment>

<file context>
@@ -788,6 +788,13 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
                 if (codegenModel.getParentModel() != null) {
                     codegenModel.parentRequiredVars = new ArrayList<>(codegenModel.getParentModel().requiredVars);
+
+                    codegenModel.requiredVarsOptionalInParent = codegenModel.requiredVars.stream()
+                            .filter(cp ->
+                                codegenModel.getParentModel().getVars().stream()
</file context>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] [JAVA] Generating wrong constructor

1 participant